﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;

public partial class Admin_EditSlider : System.Web.UI.Page
{
    SliderController sliderController = new SliderController();
    protected void Page_Load(object sender, EventArgs e)
    {
        if(!IsPostBack)
        {
            string id = Request.QueryString["id"];
            if(id=="" || id==null|| string.IsNullOrWhiteSpace(id.ToString()) || string.IsNullOrEmpty(id.ToString()))
            {
                error();
            }
            else
            {
                int idd = Convert.ToInt32(id);
                DataTable dt = sliderController.GetByID(idd);
                Slider s = new Slider(dt.Rows[0]);
                txtTitle.Text = s.Title;
                txtOrder.Text = s.Order + "";
                txtUrl.Text = s.Url;
                image_slider.ImageUrl = ResolveUrl("~/")+s.Image;
                if(s.Status)
                {
                    ddlStatus.SelectedIndex = 0;
                }
                else
                {
                    ddlStatus.SelectedIndex = 1;
                }
            }
            
        }
    }
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        string id = Request.QueryString["id"];
        int idd = Convert.ToInt32(id);
        Slider s = new Slider();
        s.Slider_id = idd;
        s.Title = txtTitle.Text;
        s.Url = txtUrl.Text;
        s.Order = Convert.ToInt32(txtOrder.Text);
        if(ddlStatus.SelectedIndex==0)
        {
            s.Status = true;
        }
        else
        {
            s.Status = false;
        }
        s.DateUpdate = DateTime.Now;
        s.Image = slider_value.Value;
        if(sliderController.Update(s)>0)
        {
            
            Response.Redirect(Request.Url.AbsoluteUri);
            Response.Write("<script>alert('Cập nhật slider thành công')</script>");
        }
        else
        {
            Response.Write("<script>alert('Cập nhật slider gặp lỗi')</script>");
        }
    }
    private void error()
    {
        Response.Redirect("~/Error.aspx");
    }
}